package com.nidu.demo.file.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.constants.Constants;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.file.convertor.FileConvertor;
import com.nidu.demo.file.dto.FileCO;
import com.nidu.demo.file.gateway.FileGateway;
import com.nidu.demo.file.model.File;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 文件单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FileSingleQryExe {

    private final FileGateway fileGateway;
    private final FileConvertor fileConvertor;

    /**
     * 根据ID查询文件
     *
     * @param id 文件ID
     * @return 文件信息
     */
    public SingleResponse<FileCO> execute(Long id) {
        File file = fileGateway.getById(id);
        if (file == null) {
            throw new BizException(ErrorCodeConstants.FILE_NOT_FOUND.getCode(), ErrorCodeConstants.FILE_NOT_FOUND.getMessage());
        }
        
        FileCO fileCO = fileConvertor.toClientObject(file);
        return SingleResponse.of(fileCO);
    }
}
